package com.choudou5.solr.utils;

import com.choudou5.solr.bean.common.PageBean;
import com.choudou5.solr.framework.util.RequestUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @name 查询构建器
 * @author choudou5
 * @date 2018/7/17
 */
public class QueryBuilder {

    /**
     * 查询字段 前缀<br>
     * 查询类型：EQ=等于， NEQ=不等于，RG=范围，EGT=大于等于，ELT=小于等于, LK=模糊
     */
    protected static final String Q_F_PREFIX = "QF__"; //格式：QF__{查询类型}__{字段名}

    //分页
    protected static final int MAX_PAGE_SIZE = 100;

    public static PageBean buildPageBean(HttpServletRequest req) {
        return buildPageBean(req, 10);
    }

    public static PageBean buildPageBean(HttpServletRequest req, int defPageSize) {
        PageBean pageBean = new PageBean();
        Integer pageNo = RequestUtil.getIntParameter(req, "pageNo", 1);
        Integer pageSize = RequestUtil.getIntParameter(req, "pageSize", defPageSize);
        Integer draw = RequestUtil.getIntParameter(req, "draw");
        //datatables分页
        if(draw != null){
            int start = RequestUtil.getIntParameter(req, "start", 0);
            pageSize = RequestUtil.getIntParameter(req, "length", defPageSize);
            pageNo = start==0?1:((start/pageSize)+1);
            pageBean.setPageNo(pageNo);
            pageBean.setPageSize(pageSize);
        }else{
            if(pageSize > MAX_PAGE_SIZE)
                pageSize = 20;
            pageBean.setPageNo(pageNo);
            pageBean.setPageSize(pageSize);
        }
        return pageBean;
    }
}
